<template>
  <div class="user-list-vue">
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form :inline="true" :model="searchParam" class="search-form-box">
      <el-form-item label="搜索类型">
        <el-select placeholder="请选择" style="width: 150px">
          <el-option label="ID" :value="1"></el-option>
          <el-option label="账号" :value="2"></el-option>
          <el-option label="用户名" :value="3"></el-option>
          <el-option label="手机" :value="4"></el-option>
          <el-option label="邮箱" :value="5"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键字">
        <el-input
          v-model="searchParam.keyword"
          placeholder="请输入关键字"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getUserList">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="dialogFormVisible = true"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <div class="searchLoading" v-loading="searchLoading">
      <el-table :data="searchResultList">
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column prop="userNo" label="账号"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="userName" label="用户名"></el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>
        <el-table-column prop="sex" label="性别">
          <template #default="scope">{{
            scope.row.sex == 0 ? "女" : "男"
          }}</template>
        </el-table-column>
        <el-table-column prop="creationTime" label="创建时间"></el-table-column>
        <el-table-column label="状态">
          <template #default="scope">{{
            scope.row.delFlag == 0 ? "正常" : "停用"
          }}</template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button
              size="default"
              plain
              type="primary"
              v-on:click="editUserInfo(scope.row)"
              >修改</el-button
            >
            <el-button
              size="default"
              plain
              type="success"
              v-on:click="editUserAuthFun(scope.row)"
              >权限</el-button
            >
            <el-button
              size="default"
              plain
              type="warning"
              v-on:click="resetPassword(scope.row)"
              >重置密码</el-button
            >
            <el-button
              size="default"
              plain
              type="danger"
              v-on:click="deleteUser(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="page-info-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[20, 50, 100]"
        :page-size="20"
        :current-page="searchParam.pageNum"
        layout="prev, pager, next, jumper, sizes, total"
        :total="totalCount"
      >
      </el-pagination>
    </div>

    <!-- 编辑权限 -->
    <el-dialog
      title="权限编辑"
      v-model="editUserAuthDialogVisible"
      width="600px"
      :close-on-click-modal="false"
    >
      <el-form ref="form" label-width="80px">
        <el-form-item label="账号">
          <el-input v-model="editUserForm.userNo" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="editUserForm.userName" disabled></el-input>
        </el-form-item>

        <el-form-item label="权限">
          <el-select  v-model="editUserAuth" multiple filterable  placeholder="请选择" style="width: 100%" >
            <el-option v-for="item in allUserAuth" :key="item.id" :label="item.authDesc" :value="item.id" ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="editUserAuthSave">确定</el-button>
          <el-button @click="editUserAuthDialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 新增 修改用户 dialog -->
    <el-dialog
      v-model="dialogFormVisible"
      :title="editUserForm.id > 0 ? '修改用户' : '创建用户'"
      width="500"
    >
      <el-form :model="editUserForm" label-width="140px">
        <el-form-item label="账号">
          <el-input v-model="editUserForm.userNo" autocomplete="on" />
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="editUserForm.userName" autocomplete="on" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="editUserForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="editUserForm.email"></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input
            v-model="editUserForm.password"
            placeholder="为空代表不修改密码"
          ></el-input>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="editUserForm.delFlag"
            :active-value="0"
            active-text="正常"
            :inactive-value="2"
            inactive-text="停用"
          />
        </el-form-item>

        <el-form-item label="性别">
          <el-radio-group v-model="editUserForm.sex">
            <el-radio :value="1">男</el-radio>
            <el-radio :value="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">Cancel</el-button>
          <el-button type="primary" @click="updateEditUser">
            Confirm
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ArrowRight } from "@element-plus/icons-vue";
import { ref, onMounted } from "vue";
import consoleApi from "@/common/api/console";

let searchResultList = ref([
  {
    id: 1,
    userNo: "11",
    userName: "22",
    phone: "33",
    email: "44",
    password: "55",
    delFlag: 0,
    sex: 1,
  },
]);
const dialogFormVisible = ref(false);
const searchLoading = ref(false);
const searchParam = ref({
  type: 1,
  keyword: "",
  pageSize: 20,
  pageNum: 1,
});
// 分页数据
let totalCount = ref(0);
const handleSizeChange = (val: number) => {
  searchParam.value.pageSize = val;
};

const handleCurrentChange = (val: number) => {
  searchParam.value.pageNum = val;
};

//新增数据
let editUserForm = ref({
 
});

// method
const getUserList = () => {
  // console.log(import.meta.env.VITE_APP_BASE_API)
  consoleApi.getUserInfoList(searchParam.value).then((result) => {
    searchResultList.value = result.data;
    totalCount = result.total;
    // // 让加载动画停留一会
    // setTimeout(()=>{searchLoading = false;}, 500);
    // // totalCount = result.total;
    // searchResultList = result.data;
  });
};
// 弹窗

// 弹窗-权限
let editUserAuth = ref([])
// api调用
const editUserAuthSave=()=>{

}
let allUserAuth= ref([])
const editUserAuthDialogVisible = ref(false);
const editUserAuthFun = (row) => {
  //  权限弹窗打开
  editUserAuthDialogVisible.value = true;
  
  // 查询权限
   var param = {userIds: row.id};
   consoleApi.userAuthList(param).then((json)=>{
      editUserAuth = [];
      allUserAuth = json.data;
      editUserAuthDialogVisible.value = true;
      editUserForm.value = JSON.parse(JSON.stringify(row));
      for (var i = 0; i < allUserAuth.length; i++) {
        if (allUserAuth[i].checked == 1) {
          editUserAuth.push(allUserAuth[i].id);
        }
      }
   })
   

};
const addUserInfo = () => {
  dialogFormVisible.value = true;
};

const updateEditUser = () => {
  console.log(editUserForm.value);
};



// 页面加载完成 开始初始化数据
onMounted(() => {
  getUserList();
});
</script>

<script lang="ts"></script>

<style scoped lang="scss">
.user-list-vue .search-form-box {
  margin-top: 10px;
}
</style>
